<template>
    <div class="app-container dialog">
        <div class="title">系统升级/变更</div>
        <div class="Subhead">基本信息</div>
        <el-form
            :model="form"
            :rules="rules"
            ref="ruleForm"
            label-position="right"
            label-width="100px"
            class="demo-ruleForm"
        >
            <el-form-item label="标题" prop="title">
                <el-input readonly style="width: 760px" v-model="form.title"></el-input>
            </el-form-item>
            <el-row>
                <el-col :span="6">
                    <el-form-item label="联系人" prop="contact_person">
                        <el-input
                            readonly
                            v-model="form.contact_person"
                        ></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="负责人" prop="person_in_charge">
                        <el-input
                            readonly
                            v-model="form.person_in_charge"
                        ></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="联系方式" prop="contact_information">
                        <el-input
                            readonly
                            v-model="form.contact_information"
                        ></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="4">
                    <el-form-item label="单位审核" prop="unit_audit">
                        <el-radio-group
                            @change="UnitAuditChange"
                            v-model="form.unit_audit"
                        >
                            <el-radio disabled label="true">是</el-radio>
                            <el-radio disabled label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item
                        v-if="IsUnitAudit"
                        label="审核单位"
                        prop="Auditunit"
                    >
                        <el-cascader
                            disabled
                            :props="{ value: 'id', checkStrictly: true }"
                            :show-all-levels="false"
                            @change="deptChange"
                            v-model="officeId"
                            placeholder="请选择，可搜索"
                            :options="dept"
                            clearable
                            filterable
                        ></el-cascader>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item
                        v-if="IsUnitAudit"
                        label="审核人员"
                        prop="Auditunit"
                    >
                        <el-select
                                disabled
                                clearable
                                style="width: 200px"
                                v-model="form.audit_person"
                                :placeholder="
                                    form.officeId
                                        ? '请选择审核人员'
                                        : '请先选择审核部门'
                                "
                            >
                                <el-option
                                    v-for="item in users"
                                    :key="item.userName"
                                    :label="item.nickName"
                                    :value="item.userName"
                                >
                                </el-option>
                            </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <div class="Subhead">需求信息</div>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="应用系统" prop="app_system">
                        <el-select
                            disabled
                            clearable
                            filterable
                            v-model="form.app_system"
                        >
                            <el-option
                                v-for="item in xtList"
                                :key="item.id"
                                :label="item.projectname"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="影响范围" prop="scope_of_influence">
                        <el-select
                            readonly
                            clearable
                            filterable
                            @change="ChangeScopeInfluence"
                            v-model="form.scope_of_influence"
                        >
                            <el-option
                                v-for="item in ScopeInfluenceList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="升级/变更时间" prop="up_change_time">
                        <el-date-picker
                            readonly
                            v-model="form.up_change_time"
                            type="datetimerange"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            placeholder="选择日期时间"
                            format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            align="right">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col v-if="isinfluence" :span="6">
                    <el-form-item label="影响时间" prop="influence_time">
                        <el-date-picker
                            readonly
                            v-model="form.influence_time"
                            type="datetimerange"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            placeholder="选择日期时间"
                            format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            align="right">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <div class="tabletitle">
                IP地址明细
                <img
                    v-if="false"
                    style="width: 20px; cursor: pointer; vertical-align: middle"
                    @click="add()"
                    src="../../../assets/icons/add.png"
                    alt=""
                />
            </div>
            <Table :columns="columns" :data="data">
                <template slot-scope="{ row, index }" slot="Type">
                    <el-select
                        disabled
                        clearable
                        filterable
                        v-model="data[index].Type"
                    >
                        <el-option
                            v-for="item in TypeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </template>
                <template slot-scope="{ row, index }" slot="ChangeTitle">
                    <Input
                        readonly
                        type="text"
                        v-model="data[index].ChangeTitle"
                    />
                </template>
                <template slot-scope="{ row, index }" slot="ContentBeforeChange">
                    <Input
                        readonly
                        type="text"
                        v-model="data[index].ContentBeforeChange"
                    />
                </template>
                <template slot-scope="{ row, index }" slot="ContentAfterChange">
                    <Input
                        readonly
                        type="text"
                        v-model="data[index].ContentAfterChange"
                    />
                </template>
                <template slot-scope="{ row, index }" slot="BackupStatus">
                    <el-select
                        disabled
                        clearable
                        filterable
                        v-model="data[index].BackupStatus"
                    >
                        <el-option
                            v-for="item in BackupStatusList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </template>
                <template slot-scope="{ row, index }" slot="changeTime">
                    <el-date-picker
                        readonly
                        style="width: 100%;"
                        v-model="form.changeTime"
                        align="right"
                        type="date"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期"
                        :picker-options="pickerOptions">
                    </el-date-picker>
                </template>
                <template slot-scope="{ row, index }" slot="remarks">
                    <Input
                        readonly
                        type="text"
                        v-model="data[index].remarks"
                        v-if="editIndex !== index"
                    />
                </template>

                <template v-if="false" slot-scope="{ row, index }" slot="action">
                    <Button type="error" @click="remove(index)">删除</Button>
                </template>
            </Table>
            <el-form-item label="需求内容">
                <editor
                :readonly="true"
                    v-model="form.demand_content"
                    style="
                        min-height: 192px;
                        max-height: 400px;
                        overflow-y: auto;
                    "
                    :min-height="192"
                    :max-height="400"
                />
            </el-form-item>
        </el-form>
        <div class="Subhead">
            附件
            <el-upload
                v-if="false"
                style="display: inline-block"
                class="upload-demo"
                accept
                list-type="picture"
                :show-file-list="false"
                ref="upload"
                :file-list="fileList"
                action=""
                :on-remove="handleChange"
                :on-change="handleChange"
                :auto-upload="false"
            >
                <img
                    style="width: 20px; cursor: pointer; vertical-align: middle"
                    src="../../../assets/icons/add.png"
                    alt=""
                />
            </el-upload>
        </div>
        <el-table :data="fileList">
            <el-table-column label="文件名称">
                <template slot-scope="scope">
                    {{
                        scope.row.fileName
                    }}
                </template>
            </el-table-column>
            <el-table-column label="文件类型">
                <template slot-scope="scope">
                    {{
                        scope.row.fileExt
                    }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                width="100"
                v-if="false"
                class-name="small-padding fixed-width"
            >
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-view"
                      v-show="scope.row.fileExt == 'jpeg' ||
                                  scope.row.fileExt == 'jpg' ||
                                  scope.row.fileExt == 'png' ||
                                  scope.row.fileExt == 'pdf' ||
                                  scope.row.fileExt == 'gif'"
                      @click="previewFile(scope.row)"
                      >预览
                  </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="DeleteFile(scope.row.id)"
                        >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :visible.sync="dialogVisible">
            <div style="max-height: 90vh; overflow-y: scroll">
                <img width="100%" :src="dialogImageUrl" alt="" />
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import Editor from "@/components/Editor";
import { AllcloudUpList } from "@/api/cloud/cloud";
import { definition } from "@/api/WorkOrder/OperationMaintenance.js"
import { listUser2 } from "@/api/system/user";
import { uploadFile } from "@/api/system/upload.js";
import { Getformdata,taskComplete } from "@/api/todoItem/todo.js";
import {
    DelectUploadFile
  } from "@/api/cloud/cloud";
export default {
    name: "SystemUpgradesDonedetails",
    components: {
        Editor,
    },
    data() {
        return {
          dialogVisible:false,
          dialogImageUrl:'',
          base_url: process.env.VUE_APP_BASE_API,
            loading:false,
            taskName:'',
            title:'',
            taskId:'',
            definitionName:'',
            btnLoading:true,
            //按钮
            form: {
                unit_audit:'false',
                audit_unit:null,
                audit_person:null,
            },
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                    picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24);
                    picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                    const date = new Date();
                    date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', date);
                    }
                }]
            },
            isinfluence:false,
            IsUnitAudit:false,
            //上云系统列表
            xtList: [],
            // 类型下拉数据列表
            TypeList:[
                {
                    id:'系统升级',
                    name:'系统升级'
                },
                {
                    id:"系统变更",
                    name:'系统变更'
                }
            ],
            // 备份状态下拉数据类别
            BackupStatusList:[
                {
                    id:'已备份完成',
                    name:'已备份完成'
                },
                {
                    id:'未备份',
                    name:'未备份'
                },
                {
                    id:'无需备份',
                    name:'无需备份'
                }
            ],
            fileList: [],
            rules: {},
            applicationSystem: null,
            IpType: null,
            PortManage: null,
            port: null,
            portType: null,
            remarks: null,
            officeId:'',
            users:[],
            columns: [
                {
                    title: "类型",
                    slot: "Type",
                },
                {
                    title: "变更主题/IP地址",
                    slot: "ChangeTitle",
                },
                {
                    title: "变更前内容",
                    slot: "ContentBeforeChange",
                },
                {
                    title: "变更后内容",
                    slot: "ContentAfterChange",
                },
                {
                    title: "备份状态",
                    slot: "BackupStatus",
                },
                {
                    title: "变更时间",
                    slot: "changeTime",
                },
                {
                    title: "备注",
                    slot: "remarks",
                },
                {
                    title: "操作",
                    slot: "action",
                },
            ],
            ScopeInfluenceList:[
                {
                    id:'无影响',
                    name:'无影响'
                },
                {
                    id:'影响时间',
                    name:'影响时间'
                },
                {
                    id:'部分租户',
                    name:'部分租户'
                },
                {
                    id:'资源地',
                    name:'资源地'
                },
                {
                    id:'全平台',
                    name:'全平台'
                }
            ],
            data: [],
            editIndex: -1, // 当前聚焦的输入框的行数
            editapplicationSystem:'',
            editIpType:'',
            editPortManage:'',
            editport:'',
            editportType:'',
            editremarks:'',
            fileList:[],
            dept:[]
        };
    },
    created() {
        this.Getdata()
    },
    computed: {
        visitedViews() {
            return this.$store.state.tagsView.visitedViews
        },
        routerUrl(){
            return this.$route.path
        }
    },
    watch: {
        visitedViews(value) {
            if(value){
                if(this.$options.name == 'SystemUpgradesDonedetails'){
                    return
                }
                this.$destroy(this.$options.name)
            }
        },
        routerUrl(value){
            if(value == '/businessassistance/SystemUpgrades/Donedetails'){
                this.Getdata()
            }
        }
    },
    methods: {
      previewFile(item){
         this.dialogImageUrl = this.base_url + item.filePath;
         this.dialogVisible = true;
      },
        Getdata(){
            this.GetListSelect()
            var list = JSON.parse(sessionStorage.getItem('SystemUpgradesDonedetails'));
            this.btnLoading = false
            Getformdata(list.processInstanceId).then(res=>{
                this.form = res.data
                this.fileList = res.data.files
                if(typeof(this.form.up_change_time) == 'string'){
                    this.form.up_change_time = this.form.up_change_time.split(',')
                }
                if(typeof(this.form.influence_time) == 'string'){
                    this.form.influence_time = this.form.influence_time.split(',')
                }
                this.ChangeScopeInfluence(this.form.scope_of_influence)
                if(this.form.unit_audit == 'true'){
                    this.IsUnitAudit = true
                    this.officeId = Number(this.form.audit_unit)
                    this.deptChange([this.form.audit_unit])
                }
                this.data = res.data.ip_detauked
            })
        },
        ChangeScopeInfluence(e){
            if(e == '影响时间'){
                this.isinfluence = true
            }else{
                this.isinfluence = false
                this.form.influence_time = null
            }
        },
        UnitAuditChange(e) {
            if(e == 'true'){
                this.IsUnitAudit = true
            }else{
                this.IsUnitAudit = false
                this.form.audit_unit = null
                this.form.audit_person = null
                this.officeId = null
            }
        },
        getName(data, id) {
            data.forEach((item) => {
                if (item.id == id) {
                    this.form.officeName = item.label;
                } else if (item.children && item.children.length) {
                    this.getName(item.children, id);
                }
            });
        },
        deptChange(e) {
            this.users = [];
            if (e.length) {
                this.form.audit_unit = e[e.length - 1];
                this.getName(this.dept, this.form.audit_unit);
                listUser2({
                    deptId: this.form.audit_unit,
                    pageNum: 1,
                    pageSize: 10000000,
                }).then((response) => {
                    this.users = response.rows;
                });
            }
        },
        GetListSelect() {
            treeselect({}).then((response) => {
                this.dept = response.data;
            });
            AllcloudUpList().then((response) => {
                this.xtList = response.rows;
            });
        },
        handleChange(row,fileList) {
            this.fileList = fileList;
        },
        DeleteFile(id) {
            DelectUploadFile(id).then(res => {
                if (res.code == 200) {
                    this.form.files = this.form.files.filter(item => {
                    if (item.id == id) {

                    } else {
                        return item
                    }
                    })
                    this.fileList = this.fileList.filter(item => {
                    if (item.id == id) {

                    } else {
                        return item
                    }
                    })
                    this.$message({
                    message: '删除成功',
                    type: 'success'
                    });
                }
            })
        },
        add() {
            let addObj = JSON.parse(JSON.stringify({
                Type:'系统升级',
                remarks:''
            }))
            this.data.push(addObj);
        },
        handleSave(row,index) {
            this.data[index] = row
        },
        remove(index) {
            this.$confirm('此操作将永久删除该行数据, 是否继续?', '提示', {
            confirmButtonText: '删除',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                this.data.splice(index, 1);
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        sumit(){
            if(this.fileList.length > 0){
                let Files = new FormData()
                    Files.append("key", "file");
                    this.fileList.forEach((item) => {
                        if(item.raw){
                            Files.append("file", item.raw);
                        }
                    });
                uploadFile(Files).then(response=>{
                    this.fileList = []
                    this.form.ip_detauked = this.data
                    if(this.form.up_change_time){
                        this.form.up_change_time = this.form.up_change_time.join(',')
                    }
                    if(this.form.influence_time){
                        this.form.influence_time = this.form.influence_time.join(',')
                    }
                    let obj = {
                        formData: this.form,
                        formDefKey:'systemUpOrChange',
                        title:this.form.title,
                        files:response.data
                    }
                    definition(obj)
                    .then(res=>{
                        if(res.code == 200){
                            this.$message({
                                message: res.msg,
                                type: 'success'
                            })
                            this.form = {}
                            this.data = []
                            this.form.audit_unit = null
                            this.form.audit_person = null
                        }
                    })
                    .catch(err=>{
                        if(typeof(this.form.up_change_time) == 'string'){
                            this.form.up_change_time = this.form.up_change_time.split(',')
                        }
                        if(typeof(this.form.influence_time) == 'string'){
                            this.form.influence_time = this.form.influence_time.split(',')
                        }
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        })
                    })
                })
            }else{
                this.form.ip_detauked = this.data
                let obj = {
                    formData: this.form,
                    formDefKey:'systemUpOrChange',
                    title:this.form.title,
                }
                definition(obj).then(res=>{
                    if(res.code == 200){
                        this.$message({
                            message: res.msg,
                            type: 'success'
                        })
                        this.form = {}
                        this.data = []
                        this.form.audit_unit = null
                        this.form.audit_person = null
                    }
                })
            }
        },
        changeExamineShow(num,leble){
            this.$prompt('请输入审批意见', num == 1 ? '通过': '不通过', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            inputValue:num == 1 ? '通过': '不通过',
            }).then((resp) => {
                this.form.ip_detauked = this.data
                if(this.form.up_change_time){
                    this.form.up_change_time = this.form.up_change_time.join(',')
                }
                if(this.form.influence_time){
                    this.form.influence_time = this.form.influence_time.join(',')
                }
                taskComplete({
                    taskId: this.taskId,
                    actionName: num == 1 ? '通过': '不通过',
                    action: num == 1 ? 'COMPLETE': 'BACK',
                    formData: this.form,
                    formDefKey:this.form.formDefKey,
                    files:this.fileList,
                    comment:resp.value
                }).then(res=>{
                    if(res.code == 200){
                        this.$message({
                            type: 'success',
                            message: res.msg
                        }); 
                        this.$store.state.tagsView.visitedViews.splice(this.$store.state.tagsView.visitedViews.findIndex(item => item.path === this.$route.path), 1)
                        this.$destroy(this.$options.name)
                        this.$router.push('/todoItem/todo')
                    }
                }).catch(err=>{
                        if(typeof(this.form.up_change_time) == 'string'){
                            this.form.up_change_time = this.form.up_change_time.split(',')
                        }
                        if(typeof(this.form.influence_time) == 'string'){
                            this.form.influence_time = this.form.influence_time.split(',')
                        }
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        })
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                });          
            });
        }
    },
};
</script>

<style scoped lang="scss">
.dialog {
    .title {
        text-align: left;
        margin-bottom: 20px;
        font-size: 18px;
        font-weight: bold;
        padding-left: 4px;
        border-left: 4px solid #38b3aa;
    }
    .Subhead {
        text-align: left;
        margin-bottom: 20px;
        font-size: 14px;
        font-weight: bold;
        padding-left: 4px;
        border-left: 2px solid #38b3aa;
    }
    .tabletitle {
        width: 100%;
        text-align: center;
        font-weight: bold;
        border: 1px solid rgb(248, 248, 249);
        height: 40px;
        line-height: 40px;
        border-bottom: none;
    }
}
</style>